using FarPoint.Win.Spread;
using FarPoint.Win.Spread.CellType;
using GrapeCity.Spreadsheet;
using GrapeCity.Spreadsheet.Drawing;
using Spread.Common.Features.Sample;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Spread.NewShapeEngine.Features
{
    public partial class EnhancedCameraShape : BaseSampleControl
    {
        public EnhancedCameraShape()
        {
            InitializeComponent();
        }
        private void EnhancedCameraShape_Load(object sender, EventArgs e)
        {
            fpSpread1.Features.EnhancedShapeEngine = true;
            fpSpread1.Sheets.Count = 4;

            InitAreaSparkline(fpSpread1.Sheets[3]);
            InitPieSparkline(fpSpread1.Sheets[2]);
            InitParetoSparkline(fpSpread1.Sheets[1]);
            InitCameraShape(fpSpread1.Sheets[0]);
        }

        private void InitCameraShape(SheetView sheetView)
        {
            var worksheet = sheetView.AsWorksheet();
            var workbook = worksheet.Workbook;
            worksheet.Name = "CameraShape";

            worksheet.SetValue(1, 0, "Select :");
            worksheet.Cells["B2"].CellType = new ComboBoxCellType()
            {
                Items = new string[] { "Late Arrivals By Reported Cause", "My Assets", "Revenue By State" },
                EditorValue = EditorValue.Index,
                StopEditingAfterDropDownItemSelected = true
            };
            worksheet.Cells["B2"].Value = 0;
            workbook.Names.Add("SelectedRange", "=IF(CameraShape!$B$2=0,Pareto!$A$1:$E$8,IF(CameraShape!$B$2=1,Pie!$A$1:$D$8,Area!$A$1:$D$7))");
            worksheet.Columns["A:B"].AutoFit();
            IShape cameraShape = worksheet.Shapes.AddPicture("", false, true, 0, 60);
            cameraShape.Formula = "SelectedRange";
        }

        private void InitParetoSparkline(SheetView sheetView)
        {
            var worksheet = sheetView.AsWorksheet();
            worksheet.Name = "Pareto";

            worksheet.SetValue(1, 0, new object[,] {
                { "Reason", "Cases", "Color", "Bar Size", "Diagram" },
                { "Traffic", 20, "#CCCCCC", 0.1, null },
                { "Child care", 15, "#82BC00", 0.3, null },
                { "Public transportation", 13, "#F7A711", 0.5, null },
                { "Weather", 6, "#00C2D6", 0.7, null },
                { "Overslept", 4, "#FFE7BA", 0.8, null },
                { "Emergency", 1, "#000000", 1, null }
            });

            worksheet.Cells["A11:I11"].Merge();
            worksheet.Cells["A11"].Value = "*Result: By the sparkline above can draw a conclusion that the reasons for 80% of the employees be late are \"traffic\", \"child care\" and \"public transportation\".";

            for (int index = 2; index < 8; index++)
            {
                worksheet.Cells[$"E{index + 1}"].Formula2 = $"PARETOSPARKLINE($B$3:$B$8,ROW()-2,$C$3:$C$8,0.5,0.8,0,2,false,'gray','orange',$C$3:$C$8,$D$3:$D$8)";
            }

            for (int i = 1; i < 4; i++)
            {
                worksheet.Columns[$"{(char)(i + 65)}"].ColumnWidth = 80;
            }

            worksheet.Columns["E"].ColumnWidth = 340;
            worksheet.Columns["A"].ColumnWidth = 140;
            worksheet.Rows["1"].RowHeight = 35;
            worksheet.Cells["A1:E1"].Merge();

            worksheet.Cells["A1"].Value = "Late Arrivals by Reported Cause";
            worksheet.Cells["A1"].VerticalAlignment = GrapeCity.Spreadsheet.VerticalAlignment.Center;
            worksheet.Cells["A1"].Font.ApplyFont(new GrapeCity.Spreadsheet.Font() { Name = "Arial", Size = 10, Bold = true });
            sheetView.Cells["A1"].BackColor = System.Drawing.Color.Gray;
            sheetView.Cells["A1"].ForeColor = System.Drawing.Color.White;

            worksheet.Cells["A2:D8"].HorizontalAlignment = GrapeCity.Spreadsheet.HorizontalAlignment.Left;
            worksheet.Cells["A2:E2"].Font.ApplyFont(new GrapeCity.Spreadsheet.Font() { Name = "Arial", Size = 10, Bold = true });
            worksheet.Cells["A2:E2"].Borders.ApplyBorder(new GrapeCity.Spreadsheet.Border()
            {
                Bottom = new GrapeCity.Spreadsheet.BorderLine(GrapeCity.Spreadsheet.BorderLineStyle.Thin, GrapeCity.Spreadsheet.Color.FromKnownColor(GrapeCity.Core.KnownColor.Black))
            });
        }

        private void InitPieSparkline(SheetView sheetView)
        {
            var worksheet = sheetView.AsWorksheet();
            worksheet.Name = "Pie";

            worksheet.Cells["A1:D1"].Merge();

            var range = worksheet.Cells["A1"];
            range.Value = "My Assets";
            range.Font.ApplyFont(new GrapeCity.Spreadsheet.Font() { Name = "Arial", Size = 17 });
            range.VerticalAlignment = VerticalAlignment.Center;

            for (int i = 65; i < 69; i++)
            {
                sheetView.Cells[$"{Convert.ToChar(i)}2"].BackColor = ColorTranslator.FromHtml("#E3E3E3");
            }

            worksheet.SetValue(1, 0, new object[,]
            {
                { "Asset Type", "Amount", "Diagram", "Note" },
                { "House", 120000, null, null },
                { "401k", 78000, null, null },
                { "Savings", 25000, null, null },
                { "Bonds", 15000, null, null },
                { "Stocks", 9000, null, null },
                { "Car", 7500, null, null }
            });

            worksheet.Cells["C3:C8"].Merge();
            worksheet.Cells["C3"].Formula2 = "PIESPARKLINE(B3:B8,\"#82bc00\",\"#96c63f\",\"#aacf62\",\"#bcd983\",\"#cee3a3\",\"#dfecc3\")";

            sheetView.Cells["D3"].BackColor = ColorTranslator.FromHtml("#82bc00");
            worksheet.Cells["D3"].Formula = "B3/SUM(B3:B8)";
            sheetView.Cells["D4"].BackColor = ColorTranslator.FromHtml("#96c63f");
            worksheet.Cells["D4"].Formula = "B4/SUM(B3:B8)";
            sheetView.Cells["D5"].BackColor = ColorTranslator.FromHtml("#aacf62");
            worksheet.Cells["D5"].Formula = "B5/SUM(B3:B8)";
            sheetView.Cells["D6"].BackColor = ColorTranslator.FromHtml("#bcd983");
            worksheet.Cells["D6"].Formula = "B6/SUM(B3:B8)";
            sheetView.Cells["D7"].BackColor = ColorTranslator.FromHtml("#cee3a3");
            worksheet.Cells["D7"].Formula = "B7/SUM(B3:B8)";
            sheetView.Cells["D8"].BackColor = ColorTranslator.FromHtml("#dfecc3");
            worksheet.Cells["D8"].Formula = "B8/SUM(B3:B8)";

            worksheet.Cells["D3:D8"].CellType = new PercentCellType()
            {
                DecimalPlaces = 2
            };

            worksheet.Cells["B3:B8"].CellType = new CurrencyCellType()
            {
                ShowCurrencySymbol = true,
                DecimalPlaces = 0,
                ShowSeparator = true,
                CurrencySymbol = "$"
            };

            worksheet.Rows["1"].RowHeight = 30;
            worksheet.Rows["2:8"].RowHeight = 25;
            worksheet.Columns["A"].ColumnWidth = 100;
            worksheet.Columns["B"].ColumnWidth = 100;
            worksheet.Columns["C"].ColumnWidth = 200;
        }

        private void InitAreaSparkline(SheetView sheetView)
        {
            var worksheet = sheetView.AsWorksheet();
            worksheet.Name = "Area";
            worksheet.Cells["A1:E1"].Merge();
            worksheet.Rows["1"].RowHeight = 32;

            worksheet.Cells["A1"].Text = "Revenue by State";
            worksheet.Cells["A1"].VerticalAlignment = GrapeCity.Spreadsheet.VerticalAlignment.Center;
            worksheet.Cells["A1"].Font.ApplyFont(new GrapeCity.Spreadsheet.Font() { Name = "Arial", Size = 17 });

            var table = worksheet.Tables.Add("A2:E6", GrapeCity.Spreadsheet.YesNoGuess.Guess, FarPoint.Win.Spread.TableStyle.TableStyleLight1);
            table.ShowAutoFilter = false;

            worksheet.SetValue(1, 0, new object[,]
            {
                { "State", 2018, 2019, 2020, "Diagram" },
                { "Idaho", 10000, -9000, 15000, null },
                { "Montana", 1000, 9000, null, null },
                { "Oregon", 10000, 7000, 0, null },
                { "Washington", 1000, 10000, 5000, null },
                { "Utah", -5000, 5000, 12000, null }
            });

            worksheet.Cells["B3:D7"].CellType = new CurrencyCellType() { DecimalPlaces = 0 };
            worksheet.Cells["E3"].Formula = "AREASPARKLINE(B3:D3,,,0,10000,\"#82bc00\",\"#f7a711\")";
            worksheet.Cells["E4"].Formula = "AREASPARKLINE(B4:D4,,,0,10000,\"#82bc00\",\"#f7a711\")";
            worksheet.Cells["E5"].Formula = "AREASPARKLINE(B5:D5,,,0,10000,\"#82bc00\",\"#f7a711\")";
            worksheet.Cells["E6"].Formula = "AREASPARKLINE(B6:D6,,,0,10000,\"#82bc00\",\"#f7a711\")";
            worksheet.Cells["E7"].Formula = "AREASPARKLINE(B7:D7,,,0,10000,\"#82bc00\",\"#f7a711\")";

            worksheet.Rows["1"].RowHeight = 50;
            worksheet.Rows["2"].RowHeight = 25;
            worksheet.Rows["3:7"].RowHeight = 35;

            worksheet.Columns["A"].ColumnWidth = 100;
            worksheet.Columns["B:D"].ColumnWidth = 80;
            worksheet.Columns["E"].ColumnWidth = 200;

        }

        private void nameManagerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            fpSpread1.ShowListCustomNameForm(this.ParentForm);
        }
    }
}


//
//EnhancedCameraShape.Designer.cs
//



namespace Spread.NewShapeEngine.Features
{
    partial class EnhancedCameraShape
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(EnhancedCameraShape));
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.splitContainer1 = new System.Windows.Forms.SplitContainer();
            this.splitContainer2 = new System.Windows.Forms.SplitContainer();
            this.nameBox1 = new FarPoint.Win.Spread.NameBox();
            this.panel1 = new System.Windows.Forms.Panel();
            this.formulaTextBox1 = new FarPoint.Win.Spread.FormulaTextBox();
            this.fpSpread1 = new FarPoint.Win.Spread.FpSpread(FarPoint.Win.Spread.LegacyBehaviors.None, ((object)(resources.GetObject("splitContainer1.Panel2.Controls"))));
            this.fpSpread1_Sheet1 = this.fpSpread1.GetSheet(0);
            this.nameManagerToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.menuStrip1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
            this.splitContainer1.Panel1.SuspendLayout();
            this.splitContainer1.Panel2.SuspendLayout();
            this.splitContainer1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
            this.splitContainer2.Panel1.SuspendLayout();
            this.splitContainer2.Panel2.SuspendLayout();
            this.splitContainer2.SuspendLayout();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).BeginInit();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.nameManagerToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(698, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // splitContainer1
            // 
            this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer1.Location = new System.Drawing.Point(0, 24);
            this.splitContainer1.Name = "splitContainer1";
            this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
            // 
            // splitContainer1.Panel1
            // 
            this.splitContainer1.Panel1.Controls.Add(this.splitContainer2);
            // 
            // splitContainer1.Panel2
            // 
            this.splitContainer1.Panel2.Controls.Add(this.fpSpread1);
            this.splitContainer1.Size = new System.Drawing.Size(698, 272);
            this.splitContainer1.SplitterDistance = 25;
            this.splitContainer1.TabIndex = 1;
            // 
            // splitContainer2
            // 
            this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.splitContainer2.Location = new System.Drawing.Point(0, 0);
            this.splitContainer2.Name = "splitContainer2";
            // 
            // splitContainer2.Panel1
            // 
            this.splitContainer2.Panel1.Controls.Add(this.nameBox1);
            // 
            // splitContainer2.Panel2
            // 
            this.splitContainer2.Panel2.Controls.Add(this.panel1);
            this.splitContainer2.Size = new System.Drawing.Size(698, 25);
            this.splitContainer2.SplitterDistance = 232;
            this.splitContainer2.TabIndex = 0;
            // 
            // nameBox1
            // 
            this.nameBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.nameBox1.Location = new System.Drawing.Point(0, 0);
            this.nameBox1.MaxLength = 255;
            this.nameBox1.Name = "nameBox1";
            this.nameBox1.Size = new System.Drawing.Size(232, 25);
            this.nameBox1.TabIndex = 0;
            // 
            // panel1
            // 
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panel1.Controls.Add(this.formulaTextBox1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(462, 25);
            this.panel1.TabIndex = 0;
            // 
            // formulaTextBox1
            // 
            this.formulaTextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.formulaTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.formulaTextBox1.Location = new System.Drawing.Point(0, 0);
            this.formulaTextBox1.Name = "formulaTextBox1";
            this.formulaTextBox1.Size = new System.Drawing.Size(460, 23);
            this.formulaTextBox1.TabIndex = 0;
            // 
            // fpSpread1
            // 
            this.fpSpread1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread1.Font = new System.Drawing.Font("Calibri", 11F);
            this.fpSpread1.Location = new System.Drawing.Point(0, 0);
            this.fpSpread1.Name = "fpSpread1";
            this.fpSpread1.Size = new System.Drawing.Size(698, 243);
            this.fpSpread1.TabIndex = 0;
            // 
            // nameManagerToolStripMenuItem
            // 
            this.nameManagerToolStripMenuItem.Name = "nameManagerToolStripMenuItem";
            this.nameManagerToolStripMenuItem.Size = new System.Drawing.Size(101, 20);
            this.nameManagerToolStripMenuItem.Text = "Name Manager";
            this.nameManagerToolStripMenuItem.Click += new System.EventHandler(this.nameManagerToolStripMenuItem_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
            this.ClientSize = new System.Drawing.Size(698, 296);
            this.pnlMain.Controls.Add(this.splitContainer1);
            this.Controls.Add(this.menuStrip1);
            this.Font = new System.Drawing.Font("Calibri", 10.875F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Margin = new System.Windows.Forms.Padding(2, 2, 2, 2);
            this.Name = "Enhanced Camera Shape";
            this.Text = "Enhanced Camera Shape";
            this.Load += new System.EventHandler(this.EnhancedCameraShape_Load);
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
            this.splitContainer1.ResumeLayout(false);
            this.splitContainer2.Panel1.ResumeLayout(false);
            this.splitContainer2.Panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
            this.splitContainer2.ResumeLayout(false);
            this.panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem nameManagerToolStripMenuItem;
        private System.Windows.Forms.SplitContainer splitContainer1;
        private System.Windows.Forms.SplitContainer splitContainer2;
        private FarPoint.Win.Spread.NameBox nameBox1;
        private System.Windows.Forms.Panel panel1;
        private FarPoint.Win.Spread.FormulaTextBox formulaTextBox1;
        private FarPoint.Win.Spread.FpSpread fpSpread1;
        private FarPoint.Win.Spread.SheetView fpSpread1_Sheet1;
    }
}
